package com.dong.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.dong.annotation.ExceptionMessageAnnotation;
import com.dong.domain.*;
import com.dong.enums.LevelEnum;
import com.dong.enums.MessageTypeEnum;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import sun.misc.IOUtils;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;

/**
 * @description: 不同类型消息推送
 * @author: DongJian
 * @create: 2020-08-29 21:51
 */
@Component
public class WechatMessageUtil {

    public static final String WEBHOOK_ADDRESS_PREFIX = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=";
    public static final String WEBHOOK_ADDRESS = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a512fa72-7934-4310-8bf0-b91e4d2bb4aa";

    /**
     * 资源加载器
     */
    final static ResourceLoader RESOURCE_LOADER = new DefaultResourceLoader(Object.class.getClassLoader());

    /**
     * 发送文件消息
     * @param webHookAddress 消息接收地址
     * @param content 文本内容
     * @throws Exception
     */
    public void sendTextMessage(String content, String webHookAddress) throws Exception {
        //发送文本消息
        Message message = new Message();
        message.setMsgtype(MessageTypeEnum.text);
        TextMessage textMessage = new TextMessage();
        textMessage.setContent("这里写文本消息内容。");
        textMessage.setMentioned_list(Arrays.asList("DongJian"));
        JSONObject text = JSONObject.parseObject(JSONObject.toJSONString(textMessage));
        message.setText(text);
        sendMessage(webHookAddress, message);
    }



    /**
     * 发送图片消息
     * @param webHookAddress 消息接收地址
     * @Parm: imgUrl 图片存放地址
     * @throws Exception
     */
    public void sendImgMessage(String imgUrl, String webHookAddress) throws Exception{
        Message message = new Message();
        //发送图片消息
        message.setMsgtype(MessageTypeEnum.image);
        ImageMessage imageMessage = new ImageMessage();
        imageMessage.setBase64(ImageManager.imageToBase64Str("/img/朱茵.jpg"));
        imageMessage.setMd5(ImageManager.getStringMd5("/img/朱茵.jpg"));
        JSONObject image = JSONObject.parseObject(JSONObject.toJSONString(imageMessage));
        message.setImage(image);
        sendMessage(webHookAddress, message);
    }

    /**
     * 发送MarkDown消息
     * @param webHookAddress
     * @param templatePath 内容(markdown文本内容)
     */
    public void sendMarkDownMessage(String webHookAddress, String templatePath, ExceptionMessageAnnotation annotation, String exceptionMessage) throws IOException, URISyntaxException {
        Message message = new Message();
        // 发送MarkDown消息
        message.setMsgtype(MessageTypeEnum.markdown);
        MarkdownMessage markdownMessage = new MarkdownMessage();
        String content = resourceLoad(templatePath);
        markdownMessage.setContent(String.format(content,annotation.moduleName(), annotation.describtion(), LevelEnum.getName(annotation.priority()), exceptionMessage));
        JSONObject markdown = JSON.parseObject(JSON.toJSONString(markdownMessage));
        message.setMarkdown(markdown);
        sendMessage(webHookAddress,message);
    }

    /**
     * 发送图文消息
     * @param webHookAddress 消息接收地址
     * @param title 标题
     * @param url 点击后跳转的链接
     * @param description 描述，不超过512个字节，超过会自动截断
     * @param picurl 图文消息的图片链接
     */
    public void sendNewsMessage(String webHookAddress, String title, String url, String description, String picurl) throws IOException, URISyntaxException {
        Message message = new Message();
        // 发送图文消息
        message.setMsgtype(MessageTypeEnum.news);
        NewsMessage newsMessage = new NewsMessage();
        List<Article> articles = Lists.newArrayList();
        Article article1 = new Article(title, url,description, picurl);
        articles.add(article1);
        newsMessage.setArticles(articles);
        JSONObject news = JSON.parseObject(JSON.toJSONString(newsMessage));
        message.setNews(news);
        sendMessage(webHookAddress, message);
    }

    /**
     * 发送文件消息
     */
    public void sendFileMessage() {

    }
    /**
     * 根据传入的webHookAddress 发送消息,为空则使用默认地址
     * @param webHookAddressID
     * @param message
     * @throws URISyntaxException
     * @throws IOException
     */
    private void sendMessage(String webHookAddressID, Message message) throws URISyntaxException, IOException {
        if (StringUtils.isBlank(webHookAddressID)) {
            MessageManager.sendMessage(message, WEBHOOK_ADDRESS);
        } else {
            MessageManager.sendMessage(message, WEBHOOK_ADDRESS_PREFIX + webHookAddressID);
        }
    }


    /**
     * 配置文件资源加载
     * @param templatePath 配置文件路径
     * @return
     */
    public static String resourceLoad(String templatePath) throws IOException {
        Resource resource = RESOURCE_LOADER.getResource(ResourceUtils.CLASSPATH_URL_PREFIX + templatePath);
        return new String(IOUtils.readFully(resource.getInputStream(),-1, true),"utf-8");
    }
}
